/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.File;
import java.util.ArrayList;
import util.RemoteFile;
import views.SearchWindow;

/**
 *
 * @author Victor
 */
public class SearchController {
    
    private MainWindowController mainController;
    private SearchWindow searchWindow;
    private DatabaseController dbController;
    
    private ArrayList<RemoteFile> availableFiles;        //Lista de arquivos disponíveis para download
    
    public SearchController(MainWindowController mainController, SearchWindow searchWindow){
        this.mainController = mainController;
        this.searchWindow = searchWindow;
        availableFiles = new ArrayList<>();
        
        //É necessário atualizar o bd de arquivos disponíveis quando construir
        
        listFiles();
    }
       
    //Lista todos os arquivos disponíveis        NECESSÁRIO APENAS PARA DEBUG!!
    public void listFiles(){
        synchronized(mainController.getDBController()) {
            mainController.getDBController().requestRemoteLists();
            mainController.getDBController().getAvailableFiles(availableFiles);    
        }
        
        if(!availableFiles.isEmpty()){   
            addFilesToSearchWindow();            
            return;
        } else {            
        }
        searchWindow.printDefaultMessage();
    }    
    
    //Lista os arquivos disponíveis após aplicar um filtro
    public void listFiles(String filter){
        synchronized(mainController.getDBController()) {
            mainController.getDBController().requestRemoteLists();
            mainController.getDBController().getAvailableFiles(availableFiles);  
        }
        
        //Aplicar filtro
        for(int i = 0; i < availableFiles.size(); i++){
            if(!availableFiles.get(i).getName().contains(filter)){
                availableFiles.remove(i);
                searchWindow.removeItem(i);
                i--;
            }
        }
        
        if(!availableFiles.isEmpty()){   
            //addFilesToSearchWindow();            
            return;
        }
        searchWindow.printDefaultMessage();
    }

    public void addDownload(int selectedIndex) {
        mainController.addDownload(availableFiles.get(selectedIndex));
    }

    private void addFilesToSearchWindow() {
        for(int i = 0; i < availableFiles.size(); i++){
                searchWindow.addItem(availableFiles.get(i).getName(), humanReadableByteCount(availableFiles.get(i).getSize()),availableFiles.get(i).getnSeeds(),availableFiles.get(i).getnLeechers());                
        }
        searchWindow.refresh();
    }
    
    //<editor-fold defaultstate="collapsed" desc="Other methods">
    public static String humanReadableByteCount(long bytes) {
        int unit = 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = ("kMGTPE").charAt(exp-1)+"";
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }
    //</editor-fold>

    
}
